package cn.lytcom.architecture.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.lytcom.architecture.MyApplication;
import cn.lytcom.architecture.R;
import cn.lytcom.architecture.api.AppRestQueue;
import cn.lytcom.architecture.event.InfoEvent;
import cn.lytcom.architecture.model.GitHub;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by lytcom on 2017/5/14.
 */

public class InfoFragment extends BaseFragment {

    @Inject
    AppRestQueue appRestQueue;

    @BindView(R.id.show_gitHub_info)
    TextView showInfo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getAppComponent().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_info, container, false);
    }

    @OnClick(R.id.get_gitHub_info)
    void getGitHubInfo() {
        final ProgressDialog progressDialog = ProgressDialog.show(getContext(), "", getString(R.string.loading));
        appRestQueue.getGitHub().observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<GitHub>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull GitHub gitHub) {
                    showInfo.setText(gitHub.getEmailsUrl());
                    EventBus.getDefault().post(new InfoEvent(gitHub.getEmailsUrl()));
                    Snackbar.make(showInfo, "Get info Success!", Snackbar.LENGTH_LONG).show();
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    progressDialog.dismiss();
                    Snackbar.make(showInfo, "Get info Fail!", Snackbar.LENGTH_LONG).show();
                }

                @Override
                public void onComplete() {
                    progressDialog.dismiss();
                }
            });
    }
}

